﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using _MetalSlug.Weapons.Units;
using _MetalSlug.Weapons.Factory;
using _MetalSlug.MyCharacters.Characters;
using _MetalSlug.Form.ScreenSystem;
using _MetalSlug.Form.ScreenSystem.Message;
using _MetalSlug.Mementoes;

namespace _MetalSlug.Form.MenuScreen
{
    public class MenuScreen : MyScreen
    {
        Texture2D _logo;
        DisplayHover Elder;

        List<Boom> Booms;
        List<MyButton> lstButtons;
        double mFadeDelay = .035;
        float mAlphaValue = 1;
        Random ran = new Random();
        bool viberate;
        int Time;
        bool isSelected;

        int currentIndex;
        protected int CurrentIndex
        {
            get { return currentIndex; }
            set 
            { 
                if (lstButtons[CurrentIndex].CurrentState != MyButton.STATE.DISABLE)
                {
                    lstButtons[CurrentIndex].CurrentState = MyButton.STATE.ENABLE;
                }

                if (lstButtons[value].CurrentState != MyButton.STATE.DISABLE)
                {
                    lstButtons[value].CurrentState = MyButton.STATE.HOVER;
                }

                currentIndex = value;
                Elder.SetPosition(new Vector2(490.0f, (CurrentIndex + 1) * 75.0f));
            }
        }

        public MenuScreen()
        {
            TypeOfWindow = TYPE.MAINMENU;
            LoadContent();
            InitialAttribute();
        }

        public override void LoadContent()
        {
            Elder = new DisplayHover();
            Booms = new List<Boom>();
            for (int i = 0; i < 1; i++)
            {
                CreateBoom();
            }

            ConfigXML x = new ConfigXML("Data/XMLconfig.xml");
            Background = GameUtil.LoadTexture(x.getBackGround());
            _logo = GameUtil.LoadTexture(x.getLogo());
            XmlNodeList nodeList = x.getTotalButton();
            List<MyScreen.TYPE> lstScreen = new List<TYPE> {MyScreen.TYPE.SELECTCHAR, 
                                                            MyScreen.TYPE.LOADING, 
                                                            MyScreen.TYPE.SETTING, 
                                                            MyScreen.TYPE.EXIT};
            lstButtons = new List<MyButton>();
            for (int i = 0; i < nodeList.Count; i++)
            {
                MyButton temp = new MyButton((XmlElement)nodeList[i]);
                lstButtons.Add(temp);
                lstButtons[i].ForScreen = lstScreen[i];
            }

            CurrentIndex = 0;
            Time = 0;
        }

        public override void InitialAttribute()
        { 
            viberate = false;
            isSelected = false;
            Elder.CurrentState = DisplayHover.STATE.ACTIVE;
            mAlphaValue = 1;
            SetStateForButton();
        }

        public void Navigator()
        {
            Elder.CurrentState = DisplayHover.STATE.CHOSEN;
            isSelected = true;
        }

        
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (viberate)
            {
                spriteBatch.Draw(Background, new Rectangle(0, -30 + Time % 20, (int)GameUtil.ScreenSize.X, (int)GameUtil.ScreenSize.Y + 30),
                                   Color.Lerp(Color.White, Color.Transparent, mAlphaValue));
              
            }
            else
            {
                spriteBatch.Draw(Background, new Rectangle(0, -30, (int)GameUtil.ScreenSize.X, (int)GameUtil.ScreenSize.Y + 30),
                            Color.Lerp(Color.White, Color.Transparent, mAlphaValue));
            }

            if (Time >= 700)
            {
                for (int i = 0; i < lstButtons.Count; i++)
                {
                    lstButtons[i].Draw(spriteBatch);
                }

                Elder.Draw(spriteBatch);
                foreach (Boom t1 in Booms)
                {
                    t1.Draw(spriteBatch);
                }
            }

            Time += 10;
            if (Time <= 500)
            {
                spriteBatch.Draw(_logo, new Rectangle(Time, 350, 400, 200),
                             Color.Lerp(Color.White, Color.Transparent, mAlphaValue));
            }
            else
            {
                spriteBatch.Draw(_logo, new Rectangle(60, 350, 400, 200),
                            Color.Lerp(Color.White, Color.Transparent, mAlphaValue));
            }
        }

        public override void Update(GameTime gameTime)     
        {        
            //Decrement the delay by the number of seconds that have elapsed since
            //the last time that the Update method was called
            mFadeDelay -= gameTime.ElapsedGameTime.TotalSeconds;
            
            //If the Fade delays has dropped below zero, then it is time to 
            //fade in/fade out the image a little bit more.

            if (!isSelected)
            {
                HandleKeyboard();
            }
            else
            {
                if (Elder.CanBeSwitchScreen())
                {
                    this.Send(lstButtons[CurrentIndex].ForScreen, MyMessage.TYPE.SHOW, null);
                }
            }

            

            if (mFadeDelay <= 0)
            {
                //Reset the Fade delay
                mFadeDelay = .0035;
                lstButtons[CurrentIndex].Update(gameTime);
                mAlphaValue = MathHelper.Clamp(mAlphaValue -= 0.01f, 0, 1);
            }

            double time = gameTime.TotalGameTime.Milliseconds;

            int countExplodedBoom = Booms.RemoveAll(item => !item.Visible);
            int countExplodingBoom = Booms.Count(item => item.CurrentState == Boom.State.EXPLODE);

            if (countExplodingBoom != 0)
            {
                viberate = true;
            }
            else
            {
                viberate = false;
            }

            for (int i = 0; i < countExplodedBoom; i++)
            {
                CreateBoom();
            }

            Elder.Update(gameTime);
            foreach (Boom boom in Booms)
            {
                boom.Update(gameTime);
            } 
        }

        public override void HandleKeyboard()
        {
            if (UserInput.isKeyClicked(Keys.Down))
            {
                do
                {
                    CurrentIndex = (CurrentIndex + 1) % lstButtons.Count;
                }
                while(lstButtons[currentIndex].CurrentState == MyButton.STATE.DISABLE);
            }

            if (UserInput.isKeyClicked(Keys.Up))
            {
                do
                {
                    CurrentIndex = (CurrentIndex - 1 + lstButtons.Count) % lstButtons.Count;
                }
                while (lstButtons[currentIndex].CurrentState == MyButton.STATE.DISABLE);
            }

            if (UserInput.isKeyClicked(Keys.Enter))
            {
                Navigator();
            }
        }

        public void CreateBoom()
        {
            Boom newBoom = (Boom)BulletStore.GetInstance().Produce(Bullet.TYPE_BULLET.BOOM_BULLET);
            newBoom.Fire(new Vector2(ran.Next(800), 0), Vector2.UnitY);
            Booms.Add(newBoom);
        }

        public override void Receive(MyScreen SendWindow, MyMessage SendMessage)
        {
            SetStateForButton();
            base.Receive(SendWindow, SendMessage);
        }

        public void SetStateForButton()
        {
            for(int i=0; i< lstButtons.Count; i++)
            {
                MyButton button = lstButtons[i];
                if (button.ForScreen == TYPE.LOADING)
                {
                    if (CareTaker.GetInstance().IsNull())
                    {
                        button.CurrentState = MyButton.STATE.DISABLE;
                    }
                    else
                    {
                        if (CurrentIndex == i)
                        {
                            button.CurrentState = MyButton.STATE.HOVER;
                        }
                        else
                        {
                            button.CurrentState = MyButton.STATE.ENABLE;
                        }
                    }
                    break;
                }
            }
        }
    }
}
